﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Suk.Common;
using Suk.Models;

namespace Suk.Service
{
    public abstract class BaseService<TEntity> where TEntity : BaseModel, new()
    {
        public SqlSugarClient db
        {
            get
            {
                return AppData.GetInstance();
            }
        }
        public virtual int Insert(TEntity entity)
        {
            return db.Insertable(entity).ExecuteReturnIdentity();
        }
        public virtual int Insert(List<TEntity> entitys)
        {
            return db.Insertable(entitys).ExecuteReturnIdentity();
        }

        public virtual int Update(TEntity entity)
        {
            return db.Updateable(entity).ExecuteCommand();
        }

        public virtual int Update(List<TEntity> entity)
        {
            return db.Updateable(entity).ExecuteCommand();
        }

        public virtual int Delete(TEntity entity)
        {
            return db.Deleteable(entity).ExecuteCommand();
        }

        public virtual int Delete(List<TEntity> entity)
        {
            return db.Deleteable(entity).ExecuteCommand();
        }

        public virtual List<TEntity> FindEntity()
        {
            return db.Queryable<TEntity>().ToList();
        }
        public virtual List<TEntity> FindEntity(TEntity m)
        {
            return db.Queryable<TEntity>().ToList();
        }

        public virtual List<TEntity> FindEntity(ref int totalCount, int pageIndex = 1, int pageSize = 10)
        {
            return db.Queryable<TEntity>().OrderBy(it => it.Id).ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public virtual List<TEntity> FindEntity(TEntity m, ref int totalCount, int pageIndex = 1, int pageSize = 10)
        {
            return FindEntity(ref totalCount, pageIndex, pageSize);
        }

    }
}
